namespace cs5643.rigidbody
{

    
    using Tao.OpenGl;

    /** 
     * Basic square block/pixel primitive for representing image-based
     * rigid objects and resolving contacts. Each Block is wrapped by a
     * disk and used for contact generation.
     * 
     * @author Doug James, March 2007. 
     */
    public class Block
    {
        /** Image row index. */
        public int i { get; private set; }

        /** Image column index. */
        public int j { get; private set; }

        /** Pixel color. */
        public Pnt3F c { get; private set; }

        /** Body-frame position --- needed for contact processing. */
        public Pnt2D p;

        /** Halfwidth of block (Note: the block radius is sqrt(2)*h). */
        public double h { get; private set; }

        /**
         * Constructs a Block.
         */
        public Block(int i, int j, Pnt3F color, Pnt2D center, double halfwidth)
        {
            this.i = i;
            this.j = j;
            this.c = color;
            this.p = new Pnt2D(center.x, center.y);
            this.h = halfwidth;
        }

        /** Image row. */
        //int i() { return i; }

        /** Image column. */
        //int j() { return j; }

        /** Center position of Block (in body coordinates). */
        //public Pnt2D p() { return p; }

        /** Halfwidth of block. Note that the block radius is sqrt(2)*h.  */
        //public double h() { return h; }

        /** Color-based mass on [0,1] with white having zero mass, and
         * darker colors approaching one (feel free to modify). */
        public double getColorMass()
        {
            double m = 1 - ((double)(c.x + c.y + c.z)) / 3.0f; // on [0,1]
            if (m < 0) m = 0;
            if (m > 1) m = 1;
            return m;
        }

        /** Draws Block geometry (using current color/lighting). */
        public void display()
        {
            double r = 0.5 * h;
            Gl.glBegin(Gl.GL_QUADS);
            Gl.glColor3d(c.x, c.y, c.z);
            Gl.glVertex3d((float)(p.x - h), (float)(p.y - h), 0);
            Gl.glVertex3d((float)(p.x + h), (float)(p.y - h), 0);
            Gl.glVertex3d((float)(p.x + h), (float)(p.y + h), 0);
            Gl.glVertex3d((float)(p.x - h), (float)(p.y + h), 0);
            Gl.glEnd();
        }
    }
}